package module.map.fileTabs
{

    import ui.Basic.UI_FileTabButton;
    import module.map.model.MapModel;
    import drongo.model.propertys.NumberProperty;
    import flash.events.Event;
    import drongo.model.events.ModelEvent;
    import module.map.model.Propertys;
    import module.map.model.ProjectModel;

    public class FileTabButton extends UI_FileTabButton
    {
        public var project:ProjectModel;
        private var stateProperty:NumberProperty;
        public function FileTabButton()
        {
            super();
        }

        public function Init(data:MapModel, project:ProjectModel):void
        {
            this.data = data;
            this.project = project;
            this.m_closeButton.addClickListener(TabButtonCloseClick);

            this.stateProperty = this.data.Get(Propertys.STATE) as NumberProperty;
            this.stateProperty.addEventListener(ModelEvent.VALUE_CHANGED, this.StateChanged);
            this.Refresh();
        }

        private function TabButtonCloseClick(e:Event):void
        {
            e.stopPropagation();
            e.stopImmediatePropagation();
            var targetData:MapModel = this.data as MapModel;
            this.project.openList.Remove(targetData);
            // 选择最后一位
            if (this.project.currentMap == targetData)
            {
                if (this.project.openList.length > 0)
                {
                    this.project.currentMap = this.project.openList.GetAt(this.project.openList.length - 1) as MapModel;
                }
                else
                {
                    this.project.currentMap = null;
                }
            }
        }

        private function StateChanged(e:ModelEvent):void
        {
            this.Refresh();
        }

        private function Refresh():void
        {
            if (this.data.Get(Propertys.STATE).GetValue() > 0)
            {
                this.title = this.data.Get(Propertys.NAME).GetValue() + "*";
            }
            else
            {
                this.title = this.data.Get(Propertys.NAME).GetValue();
            }
        }
    }
}